package edu.chinasoft.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;

import edu.chinasoft.pojo.User;

/**
 *
 * @author mhao
 * @date 2020年2月12日:下午3:56:48
 * @version V1.0
 * @Description: 
*/
@WebServlet(name = "myCheckUserNameForServlet02", value = "/Servlet/checkusernameforservlet02")
public class CheckUserNameForServlet02 extends HttpServlet {
	private static Logger logger = Logger.getLogger(CheckUserNameForServlet02.class);

	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug(" CheckUserNameForServlet02 >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		logger.debug(" CheckUserNameForServlet02 >doGet end.......");
	}

	/**
	 *
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug(" CheckUserNameForServlet02 >doPost start.......");
		//后台告诉前台，返回的内容为json格式字符串
		response.setContentType("text/json;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		//获取前台传送过来的json格式字符串
		String contentRequest="";
		String elment=null;
		BufferedReader reader = request.getReader();
		while((elment=reader.readLine())!=null) {
			contentRequest+=elment;
		}
		reader.close();
		logger.info("前台传送给后台的内容为："+contentRequest+"。");
		//把json格式字符串转换成java对象
		User javaBean=JSON.parseObject(contentRequest,User.class);
		//调用服务层方法，实现业务逻辑
		javaBean.setUserName(javaBean.getUserName()+"1");
		//把java对象转换成json格式字符串
		String backString = JSON.toJSONString(javaBean, false);
		out.write(backString);
		logger.debug(" CheckUserNameForServlet02 >doPost end.......");
	}

}